package com.thti.mecool.client.activity;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TimePicker;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.command.send.user.UserSend;
import com.thti.mecool.client.util.RegexPattern;
import com.thti.mecool.client.util.StringUtil;
import com.thti.mecool.client.util.TimeUtil;
import com.thti.mecool.client.util.Util;
import com.thti.mecool.client.widget.Toolbar;


public class AddSalersActivity extends BaseActivity {
	EditText nameEditText,telNameEditText,iDEditText;
	Button startButton,endButton,saveButton,cancelButton;
	private boolean startTimeChoice=false;
	boolean isSave=false;
	SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.mecool_add_salers);
		init();
		initToolbar();
		service();
	}
	
	private void init(){
		nameEditText=(EditText)findViewById(R.id.mecool_add_salers_name);
		telNameEditText=(EditText)findViewById(R.id.mecool_add_salers_tel);
		iDEditText=(EditText)findViewById(R.id.mecool_add_salers_id);
		startButton=(Button)findViewById(R.id.mecool_add_salers_starttime);
		endButton=(Button)findViewById(R.id.mecool_add_salers_endtime);
		saveButton=(Button)findViewById(R.id.mecool_add_salers_save);
		cancelButton=(Button)findViewById(R.id.mecool_add_salers_cancel);
	}
	public void initToolbar(){
		Toolbar toolbar=new Toolbar(this);
		toolbar.setToolbarCentreText("增加促销员");
		Toolbar.left_btn.setOnClickListener(leftbuttonListener);
		Toolbar.hideRightButton();
		Toolbar.left_btn.setImageResource(R.drawable.mecool_go_back);
	}
	private OnClickListener leftbuttonListener=new ImageButton.OnClickListener(){

		@Override
		public void onClick(View v) {
			finish();
		}};
	private void service(){
		startButton.setOnClickListener(clickListener);
		endButton.setOnClickListener(clickListener);
		saveButton.setOnClickListener(clickListener);
		cancelButton.setOnClickListener(clickListener);
	}
	OnClickListener clickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch(v.getId()){
			case R.id.mecool_add_salers_starttime:
				new TimePickerDialog(AddSalersActivity.this, new OnTimeSetListener() {
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						if(hourOfDay<10){
							if(minute<10){
								startButton.setText("0"+hourOfDay+":0"+minute);
							}else{
								startButton.setText("0"+hourOfDay+":"+minute);
							}
						}else{
							if(minute<10){
								startButton.setText(hourOfDay+":0"+minute);
							}else{
								startButton.setText(hourOfDay+":"+minute);
							}
						}
						endButton.setText("");
						startTimeChoice=true;
						
					};
				}, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), true).show();
				break;
			case R.id.mecool_add_salers_endtime:
				new TimePickerDialog(AddSalersActivity.this, new OnTimeSetListener() {	
					@Override
					public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
						if(startTimeChoice){
							if(TimeUtil.judgeCurTime(1, hourOfDay+":"+minute, startButton.getText().toString())){
								if(hourOfDay<10){
									if(minute<10){
										endButton.setText("0"+hourOfDay+":0"+minute);
									}else{
										endButton.setText("0"+hourOfDay+":"+minute);
									}
								}else{
									if(minute<10){
										endButton.setText(hourOfDay+":0"+minute);
									}else{
										endButton.setText(hourOfDay+":"+minute);
									}
								}
							}else{
								Util.sendToast(AddSalersActivity.this, "不能早于开始时间");
							}
						}else{
							Util.sendToast(AddSalersActivity.this, "请先选择开始时间");
						}
					};
				}, Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), true).show();
				break;
			case R.id.mecool_add_salers_save:
				addSalers();
				break;
			case R.id.mecool_add_salers_cancel:
				Intent intent=new Intent(AddSalersActivity.this,SellDetailActivity.class);
				isSave=false;
				intent.putExtra("issave",isSave);
				setResult(RESULT_OK,intent);
				finish();
				break;
			}
		}
	};
	
	
	public void addSalers(){
		if(StringUtil.isBlank(nameEditText.getText().toString())){
			nameEditText.setError("用户名不能为空!");
			return;
		}
		if(RegexPattern.isChars(nameEditText.getText().toString())){
			nameEditText.setError("请勿输入非法字符!");
			return;
		}
		if(StringUtil.isBlank(telNameEditText.getText().toString())){
			telNameEditText.setError("电话不能为空！");
			return;
		}
		if(RegexPattern.isChars(telNameEditText.getText().toString())){
			telNameEditText.setError("请勿输入非法字符!");
			return;
		}
		if(!RegexPattern.isTel(telNameEditText.getText().toString())){
			telNameEditText.setError("必须输入正确的手机号!");
			return;
		}
		if(StringUtil.isBlank(iDEditText.getText().toString())){
			iDEditText.setError("身份证号不能为空！");
			return;
		}
		if(RegexPattern.isChars(iDEditText.getText().toString())){
			iDEditText.setError("请勿输入非法字符!");
			return;
		}
		if(!RegexPattern.isIdCardNo(iDEditText.getText().toString())){
			iDEditText.setError("请输入正确的身份证号");
			return;
		}
		if(StringUtil.isBlank(startButton.getText().toString())){
			Util.sendToast(AddSalersActivity.this,"时间不能为空!");
			return;
		}
		if(StringUtil.isBlank(endButton.getText().toString())){
			Util.sendToast(AddSalersActivity.this,"时间不能为空!");
			return;	
		}
		String name=nameEditText.getText().toString();
		String tel=telNameEditText.getText().toString();
		String id=iDEditText.getText().toString();
		String startTime=startButton.getText().toString();
		String endTime=endButton.getText().toString();
		Intent intent=new Intent(AddSalersActivity.this,SellDetailActivity.class);
		isSave=true;
		intent.putExtra("name", name);
		intent.putExtra("tel",tel);
		intent.putExtra("id",id);
		intent.putExtra("starttime",startTime);
		intent.putExtra("endtime",endTime);
		intent.putExtra("issave",isSave);
		setResult(RESULT_OK,intent);
		finish();
	}
}
